
$(function () {
    var editor = editormd("editor", {
        width: "100%",
        height: "100%",
        path: "./editor.md-master/lib/",  // Autoload modules mode, codemirror, marked... dependents libs path
    });
});
new Vue({
    data() {
        return {
            dialogFormVisible: false,
            dialogVisible: false,
            articleKinds: [],
            articleTags: [],
            form: {
                articleTitle: '',
                articleContent: '',
                articleKind: '',
                articleTag: [],
                articleSee: 0,
                userId: -1,
            }
        }
    },
    methods: {
        saveToDraftBox: function () {
            let draftId = document.getElementById("draftId").value;
            if (draftId=='null'){
                let draft = {
                    "user": {
                        "userId": this.form.userId
                    },
                    "draftContent": document.getElementById("content").innerText,
                    "draftTitle": this.form.articleTitle,
                }
                this.dialogVisible = false;
                axios.post("draft/save.do",draft).then(resp => {
                    alert("保存至草稿箱成功");
                });
            }else{
                let draft = {
                    "draftId": draftId,
                    "user": {
                        "userId": this.form.userId
                    },
                    "draftContent": document.getElementById("content").innerText,
                    "draftTitle": this.form.articleTitle,
                }
                this.dialogVisible = false;
                axios.post("draft/update.do",draft).then(resp => {
                    alert("更新草稿成功");
                });
            }
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                })
                .catch(_ => {
                });
        },
        publish: function () {
            /**
             * 正常的发布文章逻辑
             * @type {*[]}
             */
            var myArticleTag = [];
            for (var i = 0; i < this.form.articleTag.length; i++) {
                let articleTagId = this.form.articleTag[i];
                myArticleTag.push({
                    "articleTagId": articleTagId
                });
            }
            var article = {
                "user": {
                    "userId": this.form.userId
                },
                "articleKind": {
                    "articleKindId": this.form.articleKind
                },
                "articleTitle": this.form.articleTitle,
                "articleContent": document.getElementById("content").innerText,
                "articleSee": this.form.articleSee,
                "articleTag": myArticleTag
            };
            // alert(JSON.stringify(article))
            axios.post("article/add.do", article).then(resp => {
                this.$message({
                    message: '恭喜你，发布成功',
                    type: 'success'
                });
            })

            // 但是如果是草稿的话，则需要删除草稿
            let draftId = document.getElementById("draftId").value;
            if (draftId!='null'){
                axios.get("draft/delete.do?draftId="+draftId).then(resp => {
                    // alert("删除草稿成功");
                });
            }
        }
    },
    mounted() {

        alert("userId" + document.getElementById("userId").value);
        this.form.userId=document.getElementById("userId").value;

        /**
         * 请求获得文章类型
         */
        axios.get("article/getKind.do").then(resp => {
            // alert(resp.data)
            this.articleKinds = resp.data;
        })

        /**
         * 获得文章标签
         */
        axios.get("articleTag/findAllArticleTag.do").then(resp => {
            alert(resp.data)
            this.articleTags = resp.data;
        });


        /**
         * 如果是编辑草稿的话，则读取草稿内容
         */
        let draftId = document.getElementById("draftId").value;
        if (draftId!='null'){
            // var that = this;
            axios.get("article/readDraft.do?draftId="+draftId).then(resp => {
                let draft = resp.data;
                this.form.articleTitle=draft.draftTitle;
                // document.getElementById("content").innerText=draft.draftContent;
                // this.form.articleContent=draft.draftContent;
                $(function () {
                    var editor = editormd("editor", {
                        width: "100%",
                        height: "100%",
                        markdown: draft.draftContent,     // dynamic set Markdown text
                        path: "./editor.md-master/lib/",  // Autoload modules mode, codemirror, marked... dependents libs path
                    });
                });
            });
        }


    }
}).$mount("#app");